package com.yswee.mallnavi.activity.base;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.yswee.framework.manager.WindowManager;
import com.yswee.framework.utils.OSUtils;
import com.yswee.framework.widget.clickedtab.ClickedTabBar;
import com.yswee.framework.widget.clickedtab.IClickedTabContext;


public abstract class BaseClickedTabActivity extends BaseActivity implements IClickedTabContext {

	protected ClickedTabBar tabBar;
	protected ViewGroup tabContainer;
	
	protected int curTabIndex;
	protected boolean clearTabHostFocus;
	protected View[] viewList;
	
	protected abstract int getTabContainerResID();
	protected abstract int getTabBarResID();
	protected abstract int getTabCount();
	protected abstract View getTabView(int index);

	protected int getInitializeTabIndex() {
		return 0;
	}
	
	protected int getCurrentTabIndex() {
		return curTabIndex;
	}
	
	protected View getCurrentView() {
		if(viewList != null && curTabIndex >= 0 && curTabIndex < viewList.length) {
			return viewList[curTabIndex];
		}
		return null;
	}
	
	/**
	 * 初始化控件、获取内部控件
	 */
	protected void onFindView() {
		super.onFindView();
		tabContainer = (ViewGroup) findViewById(getTabContainerResID());
		tabBar = (ClickedTabBar) findViewById(getTabBarResID());
	}
	
	/**
	 * 设置监听事件
	 */
	protected void onBindListener() {
		super.onBindListener();
		if(tabBar != null) {
			tabBar.setListener(this);
		}
	}
	
	@Override
	protected void onApplyData() {
		super.onApplyData();
		viewList = new View[getTabCount()];
		curTabIndex = -1;
		onTabChanged(getInitializeTabIndex());
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (tabBar != null && tabContainer != null && 
			tabContainer.getChildCount() > 0 && clearTabHostFocus) {
			View view = tabContainer.getChildAt(0);
			if (view != null) {
				int[] xy = new int[2];
				view.getLocationOnScreen(xy);
				if (ev.getY() <= (xy[1] + view.getHeight() - tabBar.getHeight()) && ev.getY() >= xy[1]) {
					clearTabHostFocus = false;
					tabContainer.setVisibility(View.GONE);
					tabContainer.setVisibility(View.VISIBLE);
				}
			}
		}
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public void onTabChanged(int index) {
		if(curTabIndex != index && index < getTabCount()) {
			View view = viewList[index] == null ? getTabView(index) : viewList[index];
			showView(index, view);
		}
		if(tabBar != null) {
			tabBar.setTabIndex(index);
		}
		clearTabHostFocus = true;
	}
	
	protected void showView(int index, final View view) {
		for(int i = 0; i < viewList.length; i++) {
			if(viewList[i] != null) {
				if(i == index) {
					showView(viewList[i]);
				} else {
					hideView(viewList[i]);
				}
			}
		}
		if(viewList[index] == null) {
			tabContainer.addView(view);
			viewList[index] = view;
		}
		curTabIndex = index;
	}
	
	protected void showView(View v) {
		if(OSUtils.hasHoneycomb()) {
			v.setTranslationX(0);
		} else {
			v.setVisibility(View.VISIBLE);
		}
	}
	
	protected void hideView(View v) {
		if(OSUtils.hasHoneycomb()) {
			v.setTranslationX(WindowManager.get().getScreenWidth());
		} else {
			v.setVisibility(View.GONE);
		}
	}
}
